﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;

namespace JPW.OnlinePaperSubmissionSystem.Authorization
{
    /// <summary>
    /// Application's authorization provider.
    /// Defines permissions for the application.
    /// See <see cref="AppPermissions"/> for all permission names.
    /// </summary>
    public class AppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //COMMON PERMISSIONS (FOR BOTH OF TENANTS AND HOST)

            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));

            var administration = pages.CreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));
            
            var roles = administration.CreateChildPermission(AppPermissions.Pages_Administration_Roles, L("Roles"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Create, L("CreatingNewRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Edit, L("EditingRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Delete, L("DeletingRole"));
            
            var users = administration.CreateChildPermission(AppPermissions.Pages_Administration_Users, L("Users"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Create, L("CreatingNewUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Edit, L("EditingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Delete, L("DeletingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_ChangePermissions, L("ChangingPermissions"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Impersonation, L("LoginForUsers"));

            var languages = administration.CreateChildPermission(AppPermissions.Pages_Administration_Languages, L("Languages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Create, L("CreatingNewLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Edit, L("EditingLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Delete, L("DeletingLanguages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_ChangeTexts, L("ChangingTexts"));

            administration.CreateChildPermission(AppPermissions.Pages_Administration_AuditLogs, L("AuditLogs"));

            var organizationUnits = administration.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits, L("OrganizationUnits"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree, L("ManagingOrganizationTree"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers, L("ManagingMembers"));

            //TENANT-SPECIFIC PERMISSIONS

            pages.CreateChildPermission(AppPermissions.Pages_Tenant_Dashboard, L("Dashboard"), multiTenancySides: MultiTenancySides.Tenant);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Tenant_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Tenant);

            //HOST-SPECIFIC PERMISSIONS

            var editions = pages.CreateChildPermission(AppPermissions.Pages_Editions, L("Editions"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Create, L("CreatingNewEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Edit, L("EditingEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Delete, L("DeletingEdition"), multiTenancySides: MultiTenancySides.Host);

            var tenants = pages.CreateChildPermission(AppPermissions.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Create, L("CreatingNewTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Edit, L("EditingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_ChangeFeatures, L("ChangingFeatures"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Delete, L("DeletingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Impersonation, L("LoginForTenants"), multiTenancySides: MultiTenancySides.Host);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Host);
            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Maintenance, L("Maintenance"), multiTenancySides: MultiTenancySides.Host);

            #region 自定义
            var manuscriptStatus = pages.CreateChildPermission(AppPermissions.Pages_Tenant_ManuscriptStatus, L("ManuscriptStatus"), multiTenancySides: MultiTenancySides.Tenant);
            manuscriptStatus.CreateChildPermission(AppPermissions.Pages_Tenant_ManuscriptStatus_Create, L("CreateManuscriptStatus"), multiTenancySides: MultiTenancySides.Tenant);
            manuscriptStatus.CreateChildPermission(AppPermissions.Pages_Tenant_ManuscriptStatus_Delete, L("DeleteManuscriptStatus"), multiTenancySides: MultiTenancySides.Tenant);
            manuscriptStatus.CreateChildPermission(AppPermissions.Pages_Tenant_ManuscriptStatus_Update, L("UpdateManuscriptStatus"), multiTenancySides: MultiTenancySides.Tenant);
            
            

            var parameterType = pages.CreateChildPermission(AppPermissions.Pages_Tenant_ParameterType, L("ParameterType"), multiTenancySides: MultiTenancySides.Tenant);
            parameterType.CreateChildPermission(AppPermissions.Pages_Tenant_ParameterType_Create, L("CreateParameterType"), multiTenancySides: MultiTenancySides.Tenant);
            parameterType.CreateChildPermission(AppPermissions.Pages_Tenant_ParameterType_Delete, L("DeleteParameterType"), multiTenancySides: MultiTenancySides.Tenant);
            parameterType.CreateChildPermission(AppPermissions.Pages_Tenant_ParameterType_Update, L("UpdateParameterType"), multiTenancySides: MultiTenancySides.Tenant);

            var writerInformation = pages.CreateChildPermission(AppPermissions.Pages_Tenant_WriterInformation, L("WriterInformation"), multiTenancySides: MultiTenancySides.Tenant);
            writerInformation.CreateChildPermission(AppPermissions.Pages_Tenant_WriterInformation_Create, L("CreateWriterInformation"), multiTenancySides: MultiTenancySides.Tenant);
            writerInformation.CreateChildPermission(AppPermissions.Pages_Tenant_WriterInformation_Delete, L("DeleteWriterInformation"), multiTenancySides: MultiTenancySides.Tenant);
            writerInformation.CreateChildPermission(AppPermissions.Pages_Tenant_WriterInformation_Update, L("UpdateWriterInformation"), multiTenancySides: MultiTenancySides.Tenant);

            var manuscript = pages.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript, L("Manuscript"), multiTenancySides: MultiTenancySides.Tenant);
            manuscript.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript_Create, L("CreateManuscript"), multiTenancySides: MultiTenancySides.Tenant);
            manuscript.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript_Delete, L("DeleteManuscript"), multiTenancySides: MultiTenancySides.Tenant);
            manuscript.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript_Update, L("UpdateManuscript"), multiTenancySides: MultiTenancySides.Tenant);
            manuscript.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript_StartAudit, L("StartAuditManuscript"), multiTenancySides: MultiTenancySides.Tenant);
            manuscript.CreateChildPermission(AppPermissions.Pages_Tenant_Manuscript_FinalAudit, L("FinalAuditManuscript"), multiTenancySides: MultiTenancySides.Tenant);

            var announcement = pages.CreateChildPermission(AppPermissions.Pages_Tenant_Announcement, L("Announcement"), multiTenancySides: MultiTenancySides.Tenant);
            announcement.CreateChildPermission(AppPermissions.Pages_Tenant_Announcement_Create, L("CreateAnnouncement"), multiTenancySides: MultiTenancySides.Tenant);
            announcement.CreateChildPermission(AppPermissions.Pages_Tenant_Announcement_Delete, L("DeleteAnnouncement"), multiTenancySides: MultiTenancySides.Tenant);
            announcement.CreateChildPermission(AppPermissions.Pages_Tenant_Announcement_Update, L("UpdateAnnouncement"), multiTenancySides: MultiTenancySides.Tenant);

            var audit = pages.CreateChildPermission(AppPermissions.Pages_Tenant_Audit, L("Audit"), multiTenancySides: MultiTenancySides.Tenant);
            audit.CreateChildPermission(AppPermissions.Pages_Tenant_Audit_Create, L("CreateAudit"), multiTenancySides: MultiTenancySides.Tenant);
            audit.CreateChildPermission(AppPermissions.Pages_Tenant_Audit_Delete, L("DeleteAudit"), multiTenancySides: MultiTenancySides.Tenant);
            audit.CreateChildPermission(AppPermissions.Pages_Tenant_Audit_Update, L("UpdateAudit"), multiTenancySides: MultiTenancySides.Tenant);


            var instructions = pages.CreateChildPermission(AppPermissions.Pages_Tenant_Instructions, L("Instructions"), multiTenancySides: MultiTenancySides.Tenant);
            instructions.CreateChildPermission(AppPermissions.Pages_Tenant_Instructions_Create, L("CreateInstructions"), multiTenancySides: MultiTenancySides.Tenant);
            instructions.CreateChildPermission(AppPermissions.Pages_Tenant_Instructions_Delete, L("DeleteInstructions"), multiTenancySides: MultiTenancySides.Tenant);
            instructions.CreateChildPermission(AppPermissions.Pages_Tenant_Instructions_Update, L("UpdateInstructions"), multiTenancySides: MultiTenancySides.Tenant);

            var files = pages.CreateChildPermission(AppPermissions.Pages_Tenant_Files, L("Files"), multiTenancySides: MultiTenancySides.Tenant);
            files.CreateChildPermission(AppPermissions.Pages_Tenant_Files_Create, L("CreateFiles"), multiTenancySides: MultiTenancySides.Tenant);
            files.CreateChildPermission(AppPermissions.Pages_Tenant_Files_Delete, L("DeleteFiles"), multiTenancySides: MultiTenancySides.Tenant);
            files.CreateChildPermission(AppPermissions.Pages_Tenant_Files_Update, L("UpdateFiles"), multiTenancySides: MultiTenancySides.Tenant);

            #endregion

        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AbpZeroTemplateConsts.LocalizationSourceName);
        }
    }
}
